package com.hlhj.carview

import android.os.Bundle
import android.view.View
import com.example.mymvp.mvp.BaseMvpAty
import com.example.mymvp.mvp.BasePresenter
import com.example.mymvp.mvp.BaseView
import com.gyf.barlibrary.ImmersionBar
import com.hlhj.carview.utils.ScreenManager
import android.app.Activity



/**
 * Created by Never Fear   on 2018\12\18 0018.
Never More....
 */
 abstract class BaseAty<V:BaseView,P:BasePresenter<*,V>>:BaseMvpAty<V,P>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getContentID())
        ImmersionBar.with(this)
                .init()
        supportActionBar?.hide()
        presenter?.let {
            it.initView()
            it.setListener()
            it.logicStart()
        }
        findViewById<View>(R.id.btExit)?.let {
            it.setOnClickListener {
                finish()
            }
        }
        BaseAtyContainer.instance.addActivity(this)
    }


    abstract fun getContentID(): Int

}

internal class BaseAtyContainer private constructor() {

    fun addActivity(aty: Activity) {
        activityStack.add(aty)
    }

    fun removeActivity(aty: Activity) {
        activityStack.remove(aty)
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var i = 0
        val size = activityStack.size
        while (i < size) {
            if (null != activityStack[i]) {
                activityStack[i].finish()
            }
            i++
        }
        activityStack.clear()
    }

    companion object {

        val instance = BaseAtyContainer()
        private val activityStack = ArrayList<Activity>()
    }

}